define(["jquery","bbcare","fu/app/comm/page/page",
	"fu/app/specialNurse/specialNurseList.servers",
	"fu/app/specialNurse/specialNurseList.ui"],function($,$bb,$page,$servers,$ui){
	var specialNurseList = {};

	/**
	 * 按科研编号、手机号、姓名搜索患者时，传参有所不同
	 */
	var searchKeywordAjax = function(params){
		var reg = /^1\d{10}$/;
		if(reg.test(params.fuzzyVar)){
			params = $.extend(params,{phoneNum:params.fuzzyVar});
		}else{
			delete params.phoneNum;
		}
		$servers.ajaxPatientsQueue(params,ajaxPatientsQueueCallBack);
	};

	/**
	 * 输入姓名、手机号、科研编号进行搜索
	 */
	onKeywordClick = function(){
		var params = $(this).data();
		$(this).prev().val("");
		var keyword = $(this).prev().val();
		$.extend(params,{"page":1,"fuzzyVar":keyword});
		searchKeywordAjax(params);	
	}
	onKeywordInput = function(){
		var params = $(this).data();
		var keyword = $(this).val();
		$.extend(params,{"page":1,"fuzzyVar":keyword});
		searchKeywordAjax(params);
	}

	/**
	 * 点击患者卡片进入详情页
	 */
	var onBabyCardClick = function(){
		var followQueueId = $(this).attr("followQueueId");
		var ptSchemeId = $(this).attr("patientSchemeId");
		var patientId = $(this).attr("patientId");
		var params = {
			followQueueId: followQueueId,
			ptSchemeId: ptSchemeId,
			patientId: patientId,
		};
		var configParams = {
			followQueueId: followQueueId
		};
		$bb.openPage("specialDiseaseHide",params,JSON.stringify(configParams));
	};

	/**
	 * 请求患者接口成功后的回调函数
	 */
	var ajaxPatientsQueueCallBack = function(data,params){
		if(data.rows && data.rows.length > 0){
			$("#specialNurseList-tit").html(data.rows[0].followQueueName + "患者");
			var pagesAll = Math.ceil(data.total / params.rows);
			$ui.babyListInit({
				id:"babyList",
				list:data.rows,
				rows:params.rows
			});
			/* 翻页 */
			$page.turnInit({
				id:"page",
				pagesAll:pagesAll,
				page:params.page
			},$servers.ajaxPatientsQueue,params,ajaxPatientsQueueCallBack);
			/* 点击随访患者卡片进入详情页 */
			$("#babyList .clickbabycard").unbind("click");
			$("#babyList .clickbabycard").click(onBabyCardClick);
		}else{
			if(data && data.followQueueName) $("#specialNurseList-tit").html(data.followQueueName + "患者");
			$("#babyList").html('<div class="col-sm-12 col-md-12 babycard-null">暂无数据..</div>');
			$("#page").html("");
		}
		/* 姓名、科研编号、手机号码查询 */
		$("#searchPatient span").unbind("click");
		$("#searchPatient span").data(params);
		$("#searchPatient span").click(onKeywordClick);
		$("#searchPatient input").unbind("input propertychange");
		$("#searchPatient input").data(params);
		$("#searchPatient input").bind("input propertychange",onKeywordInput);
	}

	/**
	 * 跳转到新建患者页面
	 */
	var onAddPatient = function(){
		var params = $(this).data("params");
		$bb.openPage("specialDiseaseHide",params,JSON.stringify(params));
	}

	/**
	 * 初始化
	 */
	specialNurseList.init = function(){
		var queueId = $servers.getPageParams().followQueueId;
		var topbars = [
            {
                "icon":"fa fa-plus",
                "name":"新建患者",
                "callback":onAddPatient,
                "params":{
                	"followQueueId":queueId
                }
            }
        ];
        $bb.toolbar(topbars);

		var rows;
		if( document.body.offsetWidth < 992 ){
			rows = 2;
		}else if( document.body.offsetWidth <1200 ){
			rows = 3;
		}else{
			rows = 4;
		}
		var params = {
			"page":1,
			"rows":rows*5,
			"flag":"2",
			"queueId":queueId,
			"systemType":"01"
		};
		$servers.ajaxPatientsQueue(params,ajaxPatientsQueueCallBack);
	}

	return specialNurseList;
});
